package com.sora.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sora.commonutils.R;
import com.sora.commonutils.vo.CourseWebVoOrder;
import com.sora.eduservice.entity.EduCourse;
import com.sora.eduservice.entity.frontvo.CourseWebVo;
import com.sora.eduservice.entity.vo.CourseInfoVo;
import com.sora.eduservice.entity.vo.CoursePublishVo;
import com.sora.eduservice.entity.vo.CourseQuery;
import com.sora.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author sora
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 所有课程
     * @return
     */
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    /**
     * 添加课程消息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        System.out.println(courseInfoVo);
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    /**
     * 更新课程
     * @param courseInfoVo
     * @return
     */
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    /**
     * 获取发布课程信息
     * @param id
     * @return
     */
    @GetMapping("getpublishCourseInfo/{id}")
    public R getpublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    /**
     * 发布课程
     * @param id
     * @return
     */
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal");
        eduCourse.setId(id);
        boolean b = eduCourseService.updateById(eduCourse);
        return b ? R.ok() : R.error();
    }

    /**
     * 条件分页查询课程
     * @param page
     * @param limit
     * @param courseQuery
     * @return
     */
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> coursePage = new Page<>(page,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.eq("title",title);
        }
        eduCourseService.page(coursePage,wrapper);
        List<EduCourse> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    /**
     * 删除课程
     * @param courseId
     * @return
     */
    @DeleteMapping("removeCourseById/{courseId}")
    public R removeCourseById(@PathVariable String courseId) {
        boolean flag = eduCourseService.removeCourse(courseId);
        return flag ? R.ok() : R.error();
    }
    //根据课程id，查询课程信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public CourseWebVoOrder getCourseInfoByIdOrder(@PathVariable String courseId) {
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseId);
        CourseWebVoOrder order = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,order);
        return order;
    }
}

